package com.boc.langxiang.net;

import android.util.Log;

import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.util.L;
import com.google.gson.Gson;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * CreateTime: 2016-04-03下午9:18
 * Author: wjkjinke00@126.com
 * Description:
 */
final class MyGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {

    private final Gson gson;
    private final Type type;

    MyGsonResponseBodyConverter(Gson gson, Type type) {
        this.gson = gson;
        this.type = type;
    }


    @Override
    public T convert(ResponseBody value) throws IOException {
        String response = value.string();
        try {
            Log.d("ResponseData", response);
            BaseResponse baseResponse = gson.fromJson(response, BaseResponse.class);
            if (baseResponse.getReturnNo().equals(ResponseCode.SUCCESS)) {
                return gson.fromJson(response, type);
            } else {
                throw new ResultException(baseResponse.getReturnNo(), baseResponse.getReturnInfo());
            }
        } finally {
            value.close();
        }
    }

}
